Prerequisites
Before you begin, ensure you have:- C++17 compatible compiler (GCC 9+, Clang 10+, or MSVC 2019+)
- PVAC-HFHE cloned and included in your project (see Installation)
Build your first FHE application
1
Create your source file
Create a file named
first_fhe.cpp:2
Generate cryptographic keys
Add key generation code. This creates the public key for encryption and secret key for decryption:
Key generation takes ~859 ms. In production, you’d generate keys once and reuse them.
3
Encrypt values
Encrypt two numbers using the public key and secret key:
4
Perform homomorphic operations
Compute on encrypted data without decrypting it. The server can perform these operations without seeing the plaintext:
Privacy preserved: The server never sees 42 or 17, only encrypted ciphertexts!
5
Decrypt and verify results
The client decrypts the results using their secret key:
6
Compile and run
Compile with C++17 and optimization flags:Expected output:
Understanding the workflow
1
Key generation (one-time setup)
The client generates a key pair:
- Public key (pk): Used for encryption and homomorphic operations (can be shared publicly)
- Secret key (sk): Used for decryption (must be kept private)
2
Encryption (client-side)
The client encrypts sensitive data using both pk and sk, producing a ciphertext that reveals nothing about the plaintext.
3
Homomorphic computation (server-side)
The server performs operations on encrypted data using only the public key. It never sees the plaintext values.
4
Decryption (client-side)
The client decrypts the result using their secret key to reveal the computed value.
Try a more complex example
Polynomial evaluation
Evaluate f(x) = x³ + 2x² + 3x + 4 at x = 5, entirely on encrypted data:Text encryption
PVAC-HFHE also supports text encryption via automatic packing:Performance considerations
When to use PVAC-HFHE
When to use PVAC-HFHE
Best for:
- Scalar arithmetic: 2.9-14.3× faster than RLWE schemes (BFV/CKKS)
- Small circuit depth (d ≤ 2): Outperforms all schemes
- Addition-heavy workloads: 10-87× faster than RLWE
- Compact ciphertexts: 6-85× smaller than RLWE
- Simple ML inference: Privacy-preserving predictions
Limitations
Limitations
Not ideal for:
- Deep circuits (d ≥ 3): RLWE schemes outperform due to modulus switching
- SIMD/batching: BFV is 146× faster for batch operations
- Very deep ML models: Consider CKKS for deep neural networks
Optimization tips
Optimization tips
- Use
ct_square(pk, a)instead ofct_mul(pk, a, a)for squaring - Use
ct_mul_const()andct_add_const()when multiplying/adding by public constants - Minimize circuit depth by factoring and reusing intermediate results
- Use compiler flags:
-O2 -march=nativefor SIMD acceleration
Next steps
Core concepts
Understand the hypergraph-based encryption and LPN security
Guides
Learn advanced techniques for key generation, depth management, and optimization
Examples
Explore complete working examples including ML inference
API reference
Browse the complete API documentation